#include "settings.h"

#include <QVBoxLayout>
#include <QPushButton>
#include "qmainview.h"
#include "singlemainview.h"
#include <QDebug>
#include <QLineEdit>
#include <QDir>
#include <QFileDialog>
#include <QLabel>
#include <QJsonObject>
#include <QJsonDocument>
#include <QByteArray>
#include <QFile>
#include <QDebug>


settings::settings(QWidget *parent) : QWidget(parent)
{
    //QVBoxLayout* vlayout = new QVBoxLayout();

    QVBoxLayout* vlayout = (QVBoxLayout*)initLayout();

    QPushButton* pbutton = new QPushButton();
    pbutton->setText("点击切换");
    vlayout->addWidget(pbutton);

    connect(pbutton, SIGNAL(clicked()), this, SLOT(loadMain()));

    //_qmv = new qmainview;


    setLayout(vlayout);

    setFixedWidth(1000);
    //setFixedHeight(400);

    _smv = nullptr;
}

QLayout* settings::initLayout()
{
    QHBoxLayout* hllayout = new QHBoxLayout;
    QLabel* rootText = new QLabel;
    rootText->setText("仓库目录");
    _rootdir = new QLineEdit;
    QPushButton* rootBtn = new QPushButton;
    rootBtn->setText("修改仓库目录");

    hllayout->addWidget(rootText);
    hllayout->addWidget(_rootdir);
    hllayout->addWidget(rootBtn);


    QHBoxLayout* hrlayout = new QHBoxLayout;
    QLabel* workText = new QLabel;
    workText->setText("工作目录");
    _workdir = new QLineEdit;
    QPushButton* workBtn = new QPushButton;
    workBtn->setText("修改工作目录");
    hrlayout->addWidget(workText);
    hrlayout->addWidget(_workdir);
    hrlayout->addWidget(workBtn);

    QVBoxLayout* vlayout = new QVBoxLayout;

    vlayout->addLayout(hllayout);
    vlayout->addLayout(hrlayout);


    connect(rootBtn, SIGNAL(clicked()), this, SLOT(setRootDir()));
    connect(workBtn, SIGNAL(clicked()), this, SLOT(setWorkDir()));

    return vlayout;
}

void settings::setWorkDir(){
    QString dir = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("open path"), QDir::currentPath()));
    if(!dir.isEmpty()){
        _workdir->setText(dir);
    }
}

void settings::setRootDir(){
    QString dir = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("open path"), QDir::currentPath()));
    if(!dir.isEmpty()){
        _rootdir->setText(dir);
    }
}

bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    return false;
}

void makeConfig(QString rootdir, QString workdir){
    QVariantHash data;
    data.insert("rootdir", rootdir);
    data.insert("workdir", workdir);

    QJsonObject rootObj = QJsonObject::fromVariantHash(data);
    QJsonDocument document;
    document.setObject(rootObj);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    QString json_str(byte_array);
    QFile file("config.json");

    if(!file.open(QIODevice::ReadWrite|QIODevice::Text))
    {
       qDebug() << "file error!";
    }
    QTextStream in(&file);
    in << json_str;

    file.close();   // 关闭file
}

void settings::loadMain(){
    //_qmv->show();
    if(_smv == nullptr)
        _smv = new SingleMainView;

    QString rootdir = _rootdir->text();
    QString workdir = _workdir->text();

    if(!isDirExist(rootdir)){
        qDebug() << "仓库路径为空";
    }

    if(!isDirExist(workdir)){
        qDebug() << "工作路径为空";
    }

    makeConfig(rootdir, workdir);

    _smv->setRootAndWorkDir(_rootdir->text(), _workdir->text());
    _smv->show();
}

void settings::loadMain(QString root, QString work){
    if(_smv == nullptr)
        _smv = new SingleMainView;
    _smv->setRootAndWorkDir(root, work);
    _smv->show();
}
